﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;

namespace SuperStar.Models
{
    

    public class ResellerModels
    {
        public SuperStarEntities DB { get; set; }
        public String DEBUGTYPE { get; set; }

        public ResellerModels()
        {
            DB = new SuperStarEntities();
        }

        public List<Reseller> GetResellers()
        {
            var lReseller = from r in DB.Resellers
                          select r;
            return lReseller.ToList();
        }
        public Reseller GetResellerIdOfUsername(string username)
        {
            return  (from r in DB.Resellers
                             join u in DB.Users on r.User.UserId equals u.UserId
                             where u.UserName == username
                             select r).SingleOrDefault();

        }
        public Object GetBillsOfReseller(int resellerId, int status, int month,
                                    int startIndex, int count)
        {
           var query = DB.spReseller_GetBills(month, status, resellerId)
                            .Select(p => new{
                                Id = p.Id,
                                BillId = p.Id,
                                p.Customer_Id,
                                Customer_Name = p.User.UserName,
                                PromotionDetail = p.Product.Id,
                                Product_Id = p.Product.Id,
                                Product_Name = p.Product.Name,
                                p.Reseller_Id,
                                p.Quantity,
                                p.Price,
                                p.GiftPoint,
                                p.Status_Id,
                                p.ReceivedDate,
                                p.DateAdded
                             });
            return count > 0
                           ? query.Skip(startIndex).Take(count).ToList() //Paging
                           : query.ToList(); //No paging
        }
        public bool UpdateStatusBill(int billId, int status)
        {
            return DB.spReseller_UpdateStatusBill(billId, status) > 0;
        }
        public int GetCountBillsOfReseller(int resellerId, int status, int month)
        {
            return DB.spReseller_GetBills(month, status, resellerId).Count();
        }
    }
}
